package network.starWork.workspace;
import java.sql.SQLException;
import java.util.List;

import util.Auth4Code;
import weibo4j.Account;
import weibo4j.Friendships;
import weibo4j.Users;
import weibo4j.Weibo;
import weibo4j.model.Paging;
import weibo4j.model.User;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;
import network.starWork.workspace.database.Database;

public class Friends {
	public String TokenKey = Auth4Code.getAccessToken();

	public static void main(String[] argv){
		Friends f = new Friends();
		//Log.logDebug((f.getUserFriendsCount("1990306261")));
		String uid = "1877540477";
		f.getBilateralIds(uid);
		f.getAllFriendsByid(uid);
	}
	private String getUid(){
		Weibo weibo = new Weibo();
		weibo.setToken(TokenKey);
		Account am = new Account();
		try {
			JSONObject json = am.getUid();
			try {
				return json.get("uid").toString();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void getFriendsByid(String id ,int count, int cursor ) throws SQLException{		
		Weibo weibo = new Weibo();
		weibo.setToken(TokenKey);
		Friendships fm = new Friendships();
		try {
			String[] users = fm.getFriendsIds(id,count,cursor-1);
			for(String user : users){
				getCommonFriends(id,user,cursor);
			}				
			System.out.println("------------------------------------");
		} catch (WeiboException e) {
			e.printStackTrace();
		}	
	}
	private void getFriendsByid(String id ){		
		Weibo weibo = new Weibo();
		weibo.setToken(TokenKey);
		Friendships fm = new Friendships();
		try {
			List<User> users = fm.getFriendsByID(id);
			for(User user : users){
			}
			System.out.println(users.size());
		} catch (WeiboException e) {
			e.printStackTrace();
		}	
	}	
	
	private void getAllFriendsByid(String id){
		int scount = getUserFriendsCount(id);
		System.out.println(scount);
		for(int i = 1 ; i < (scount/500)+2;i++){
			try {
				getFriendsByid(id,500,i);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(i);
		}
	}
	
	public int getUserFriendsCount(String id){
		Weibo weibo = new Weibo();
		weibo.setToken(TokenKey);
		Users um = new Users();
		try {
			User user = um.showUserById(id);
			return user.getFriendsCount();
		} catch (WeiboException e) {
			e.printStackTrace();
		}
		return 0;
	}
	public void getCommonFriends(String uid, String fid,int cusor){
		Weibo weibo = new Weibo();
		weibo.setToken(TokenKey);
		Friendships fm = new Friendships();
		Paging page = new Paging(cusor,200);
		try {
			List<User> users = fm.getFriendsInCommon(uid, fid, page);
			String sql = "INSERT INTO `sina`.`common` (`uid`, `oid`, `fid`) VALUES (";
			Database con = Database.getInstance();  
			System.out.println(users.size());
			for(User user : users){
				//Log.logDebug(user.toString());
				String value ="\'"+uid+"\',\'"+fid+"\',\'"+user.getId()+"\')";
				con.insert(sql+value);
				
				String mysql = "INSERT INTO `sina`.`relation` (`uid`, `fid`) VALUES ("+
				"\'"+uid+"\',\'"+user.getId()+"\')";
				
				if(checkRelationInsert(uid,user.getId())==0)
					con.insert(mysql);				
			}
			if(users.size()>0){
				if(checkRelationInsert(uid,fid)==0){	
					String mysql = "INSERT INTO `sina`.`relation` (`uid`, `fid`) VALUES ("+
								"\'"+uid+"\',\'"+fid+"\')";
					con.insert(mysql);
				}
			}			
			System.out.println("------------------------------------");
		} catch (WeiboException e) {
			e.printStackTrace();
		}		
	}
	
	public void getBilateralIds(String uid){
		Weibo weibo = new Weibo();
		weibo.setToken(TokenKey);
		Friendships fm = new Friendships();
		try {
			String[] ids = fm.getFriendsBilateralIds(uid);
			String sql = "INSERT INTO `sina`.`common` (`uid`, `oid`, `fid`) VALUES (";
			Database con = Database.getInstance();			
			for(String s : ids){
				String value ="\'"+uid+"\',\'"+s+"\',\'"+s+"\')";
				con.insert(sql+value);
				
				String mysql = "INSERT INTO `sina`.`relation` (`uid`, `fid`) VALUES ("+
				"\'"+uid+"\',\'"+s+"\')";
			}
		} catch (WeiboException e) {
			e.printStackTrace();
		}		
	}
	
	public int checkRelationInsert(String uid, String fid){
		Database con = Database.getInstance();		
		String querystr = "select count(*) from relation where `uid`="+
		"\'"+uid+"\' and `fid`=\'"+fid+"\'";
		try {
			return con.count(querystr);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}
}
